<?php

namespace App\Services;

use App\Exceptions\ApiErrorException;
use App\Models\AppTypeModel;
use Illuminate\Support\Facades\Log;

/**
 * 当前请求的app的配置信息
 */
class AlipayMiniAppService extends BaseService
{
    /**
     * @var static 单例模式
     */
    protected static $instance;

    protected $miniId = '';
    /**
     * @var \Illuminate\Database\Eloquent\Model|AppTypeModel|array
     */
    protected $appConfig = [];

    public function setMiniId($miniId)
    {
        $this->miniId = $miniId;
        $appConfig = AppTypeService::getInstance()->getConfigByAppId($miniId);
        if (!$appConfig) {
            Log::error('有来自未配置的支付宝小程序进行请求', ['miniId' => $miniId]);
            throw new ApiErrorException('有来自未配置的支付宝小程序进行请求');
        }
        $this->appConfig = $appConfig;
    }

    public function getMiniId()
    {
        return $this->miniId;
    }

    public function getAppConfig($key, $default = '')
    {
        if ($key) {
            return $this->appConfig[$key] ?? $default;
        }
        return $this->appConfig;
    }
}
